<?php

namespace App\Http\Controllers;

use App\Datatables;
use App\Huati;
use App\HuatiOwner;
use App\HuatiParent;
use App\Keshi;
use App\User;
use Illuminate\Http\Request;

/**
 * Class KsAndHtController
 * @package App\Http\Controllers
 */
class KsAndHtController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }


    /**
     * 科室列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function keshiList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::KeshiList());
        }

        return view('ksandht.keshiList');
    }


    /**
     * 删除科室
     * @param Keshi $keshi
     * @return array
     * @throws \Exception
     */
    public function keshiDel(Keshi $keshi)
    {
        if (!$keshi->delete()) {
            return ['status' => 2, 'message' => '删除失败'];
        }

        return ['status' => 1, 'message' => '删除成功'];
    }

    /**
     * 修改科室
     * @param Request $request
     * @param Keshi $keshi
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function keshiModify(Request $request, Keshi $keshi)
    {
        if ($request->isMethod('post')) {
            $return = Keshi::ModifyKeshi($request, $keshi);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('ksandht.keshiModify')->with(compact('keshi'));
    }

    /**
     * 添加科室
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function keshiPublish(Request $request)
    {
        if ($request->isMethod('post')) {
            $return = Keshi::KeshiPublish($request);
            //失败
            if ($return['status'] === 2) {
                return back()->with('status',$return['status'])->with('message', $return['message']);
            }
            //成功
            return redirect(url('/keshi/keshiList'))->with('status', $return['status'])->with('message', $return['message']);
        }

        return view('ksandht.keshiPublish');
    }



    /**
     * 话题列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function huatiList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::HuatiList());
        }

        return view('ksandht.huatiList');
    }

    /**
     * 删除话题
     * @param Huati $huati
     * @return array
     * @throws \Exception
     */
    public function huatiDel(Huati $huati)
    {
        if (!$huati->delete()) {
            return ['status' => 2, 'message' => '删除失败'];
        }

        return ['status' => 1, 'message' => '删除成功'];
    }


    /**
     * 修改话题
     * @param Request $request
     * @param Huati $huati
     * @return $this|int
     */
    public function huatiModify(Request $request, Huati $huati)
    {
        if ($request->isMethod('post')) {
            $return = Huati::ModifyHuati($request, $huati);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }

        return view('ksandht.huatiModify')->with(compact('huati'));
    }


    /**
     * 添加
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function huatiPublish(Request $request)
    {
        if ($request->isMethod('post')) {
            $return = Huati::HuatiPublish($request);
            //失败
            if ($return['status'] === 2) {
                return back()->with('status',$return['status'])->with('message', $return['message']);
            }
            //成功
            return redirect(url('/huati/huatiList'))->with('status', $return['status'])->with('message', $return['message']);
        }

        return view('ksandht.huatiPublish');
    }


    ######父级话题
    /**
     * 话题列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function huatipList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::HuatipList());
        }

        return view('ksandht.huatipList');
    }

    /**
     * 删除话题
     * @param Huati $huati
     * @return array
     * @throws \Exception
     */
    public function huatipDel(HuatiParent $huati)
    {
        if (!$huati->delete()) {
            return ['status' => 2, 'message' => '删除失败'];
        }

        return ['status' => 1, 'message' => '删除成功'];
    }

    /**
     * 修改话题
     * @param Request $request
     * @param Huati $huati
     * @return $this|int
     */
    public function huatipModify(Request $request, HuatiParent $huati)
    {
        if ($request->isMethod('post')) {
            $return = HuatiParent::ModifyHuatip($request, $huati);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }

        return view('ksandht.huatipModify')->with(compact('huati'));
    }


    /**
     * 添加
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function huatipPublish(Request $request)
    {
        if ($request->isMethod('post')) {
            $return = HuatiParent::HuatipPublish($request);
            //失败
            if ($return['status'] === 2) {
                return back()->with('status',$return['status'])->with('message', $return['message']);
            }
            //成功
            return redirect(url('/huatip/huatipList'))->with('status', $return['status'])->with('message', $return['message']);
        }

        return view('ksandht.huatipPublish');
    }


    /**
     * 圈主列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function huatiOwner(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::HuatiOwner());
        }

        return view('ksandht.huatiOwner');
    }

    /**
     * 撤销圈主
     * @param HuatiOwner $huatiOwner
     * @return array
     * @throws \Exception
     */
    public function huatiOwnerDel($id)
    {
        if (!HuatiOwner::where('id', $id)->delete()) {
            return ['status' => 2, 'message' => '撤销失败'];
        }

        return ['status' => 1, 'message' => '成功'];
    }

    /**
     * 设置圈主级别
     * @param Request $request
     * @return array
     */
    public function huatiOwnerLevel(Request $request)
    {
        if (!HuatiOwner::where('id', $request->id)->update([
            'level' => $request->level
        ])) {
            return ['status' => 2, 'message' => '失败'];
        }

        return ['status' => 1, 'message' => '成功'];
    }


    /**
     * 设置圈主
     * @param Request $request
     * @return array
     */
    public function huatiOwnerAdd(Request $request)
    {
        //用户是否存在
        if (!User::where('c_id', $request->user_id)->exists()) {
            return ['status' => 2, 'message' => '该用户不存在'];
        }
        //圈子是否存在
        if (!Huati::where('id', $request->huati_id)->exists()) {
            return ['status' => 2, 'message' => '圈子不存在'];
        }
        //该用户是否是圈主
        if (HuatiOwner::where('user_id', $request->user_id)->exists()) {
            return ['status' => 2, 'message' => '该用户已是圈主'];
        }

        return HuatiOwner::HuatiOwnerAdd($request->user_id, $request->huati_id);
    }

}
